﻿/*
 * @Autor: PK
 * @Date: 2021/12/07 15:18:SS
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ShkSimulation.core.dao.statistic.feature;
using ShkSimulation.core.exceptions;
using ShkSimulation.core.optimization.extractor;

namespace ShkSimulation.core.dao.output.result {

	public class DefaultDataTableResourceResult : DataTableResourceResult {

		public readonly static Dictionary<string, Type> ColumnDefinition = new Dictionary<string, Type>() {
			{"数据采集集合Key", typeof(string)}, 
			{"变量名", typeof(string)}, 
			{"时间统计特性", typeof(string)}, 
			{"集合数据统计特性", typeof(string)}, 
			{"归属项", typeof(string)}, 
			{"值", typeof(double)}, 
		};

		public bool AddNewColumnWhenAppendRow { get; set; } = true;

		public DefaultDataTableResourceResult(Result result) : base(result) {
		}

		protected override DataColumn[] GetColumns() {
			List<DataColumn> dataColumns = ColumnDefinition.Select(each => new DataColumn(each.Key, each.Value)).ToList();
			return dataColumns.ToArray();
		}

		protected override void AppendRows(string collectionKey, VariableAndHourStatisticKey variableAndHourStatisticKey, StatisticInfo statisticInfo) {
			foreach (var (key, value) in statisticInfo) {
				DataRow row = dataTable.NewRow();
				row[0] = collectionKey;
				row[1] = variableAndHourStatisticKey.VariableName;
				row[2] = variableAndHourStatisticKey.HourStatisticName;
				row[3] = key;
				row[4] = "";
				row[5] = value.ToString("0.000");
				dataTable.Rows.Add(row);
			}
		}

		public override CellPoint GetValuePosition(int rowIndex) {
			if (dataTable == null || rowIndex < 0 || rowIndex >= dataTable.Rows.Count) {
				throw new ErrorParamException($"超过索引：{rowIndex}");
			}
			DataRow row = dataTable.Rows[rowIndex];
			string key = $"{row[0]}-{row[1]}-{row[2]}-{row[3]}";
			return new CellPoint(key, rowIndex, 5, row[5].ToString());
		}

		public override string PathInfo => "for result!";

	}
}